// pages/detail/detail.js
var common = require('../../utils/util')
const db = wx.cloud.database()
const news = db.collection('news')

Page({
  data: {
    
  },
  //添加收藏
  addFavorites:function(){
    let news = this.data.news
    wx.setStorageSync(news._id, news)
    this.setData({
      isAdd:true
    })
  },
  //取消收藏
  cancelFavorites:function(){
    let news = this.data.news
    wx.removeStorageSync(news._id)
    this.setData({
      isAdd:false
    })
  },
  //生命周期函数--监听页面加载
  onLoad: function (options) {
    let id = options.id
    //检查当前是否在收藏夹中
    var newarticle = wx.getStorageSync(id)
    //已存在
    if( newarticle != '' ){
      this.setData({
        isAdd:true,
        news:newarticle
      })
    }
    //不存在
    else{
      //根据新闻id在云数据库中查找新闻内容
      news.doc(id).get({
        success:res=>{
          console.log('ok',res.data)
          //更新页面上的新闻信息和收藏状态
          this.setData({
            news:res.data,
            isAdd:false
          })
        }
      })
    }
   
    },
})